package org.lanqiao.webfm.dao;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;

import javacommon.base.BaseJdbcDao;
import javacommon.ijdbc.RowMapper;
import javacommon.util.StringUtils;
import javacommon.web.page.Page;
import javacommon.web.page.PageRequest;

import org.lanqiao.webfm.model.Type;
import org.lanqiao.webfm.model.TypeMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
*@Author zhengwei
**/
public class TypeDao extends BaseJdbcDao<Type>{
	private static final Logger LOGGER = LoggerFactory.getLogger(TypeDao.class);
	private TypeDao(){}
	private static TypeDao instance = new TypeDao();
	public static TypeDao getInstance(){
		return instance;
	}
	public Class getEntityClass() {
		return Type.class;
	}
	
	/*public RowMapper<Type> getMapper() {
		return new TypeMapper();
	}*/
	public String getIdColumnName() {
		return "ID";
	}
	public String getTableName() {
		return "SP_TYPE";
	}
	
	public Type getById(Serializable id) throws SQLException {
		String sql = "select *"
				+ " from SP_TYPE"
				+ " where ID=?";
		Type type= operation.queryForUniqueBean(sql, getEntityClass(), id);
		if (type!=null) {
			type.setType(getById(type.getPid()));
		}
		return type;
	}
	@Override
	public void save(Type entity) throws SQLException {
		StringBuilder sql = new StringBuilder("insert into SP_TYPE")
				.append(" (")
				.append("ID").append(",")
				.append("NAME").append(",")
				.append("DESCP").append(",")
				.append("PID")
				.append(") ")
				.append("values(dev_seq.nextval,")
				.append("?").append(",")
				.append("?").append(",")
				.append("?")
				.append(")");
			
		super.operation
				.execute(sql.toString(), 
								entity.getName(),
								entity.getDescp(),
								entity.getPid()
				);
	}
	@Override
	public void update(Type entity) throws SQLException {
		StringBuilder sql = new StringBuilder("update SP_TYPE")
				.append(" set ") 
				.append("NAME=?").append(",")
				.append("DESCP=?").append(",")
				.append("PID=?")
				.append(" where ID=?");
		super.operation
				.execute(sql.toString(),
							entity.getName(),
							entity.getDescp(),
							entity.getPid(),
						entity.getId());
	}
	/**
	 * 根据query对象来查询数据，结果取决于query中有查询条件及query中的分页信息
	 */
	public Page<Type> findPage(PageRequest<Type> query) throws SQLException{
		Type type = (Type) query;
        StringBuilder sql = new StringBuilder("select * from SP_TYPE where 1=1 ");
        if(StringUtils.hasLength(type.getId())) {
         	sql.append(" and  ID =  ").append(type.getId());
        }
        if(StringUtils.hasLength(type.getName())) {
            sql.append(" and  NAME =  '").append(type.getName()).append("'");
        }
        if(StringUtils.hasLength(type.getDescp())) {
            sql.append(" and  DESCP =  '").append(type.getDescp()).append("'");
        }
        if(StringUtils.hasLength(type.getPid())) {
         	sql.append(" and  PID =  ").append(type.getPid());
        }
        if(StringUtils.hasLength(type.getSortColumns())) {
            sql.append(" order by ").append(type.getSortColumns());
        }	
        
		Page<Type>  page = pageQuery(sql.toString(),type);
		for (Type type2 : page.getResult()) {
			type2.setType(getById(type2.getPid()));
		}
		return page;
	}
	
	public void deleteByIds(String[] ids) throws SQLException {
		String[] sqls = new String[ids.length];
		for (int i = 0; i < sqls.length; i++) {
			sqls[i] = "delete from SP_TYPE where ID=" + ids[i];
		}
		operation.executeBatch(sqls);
	}
	public List<Type> findFirstLevel() throws SQLException {
		// TODO Auto-generated method stub
		return operation.queryForList("select * from sp_type where pid=0", new TypeMapper());
	}
	public List<Type> findByPtype(Type type) throws SQLException {
		return operation.queryForList("select * from sp_type where pid=?", new TypeMapper(),type.getId());
	}

}
